#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>

#include "Config.h"
#include "Common.h"
#include "WebSocketServer.h"
#include "Objects.h"
using namespace boost;
namespace RisingStar
{
	CWebSocketServer::CWebSocketServer(WORD port)				
	{
		mEndpoint.set_error_channels(websocketpp::log::elevel::all);
        mEndpoint.set_access_channels(websocketpp::log::alevel::all ^ websocketpp::log::alevel::frame_payload);

        // Initialize Asio
        mEndpoint.init_asio();
		
		        // Set the default message handler to the echo handler
        mEndpoint.set_message_handler(boost::bind(
            &CWebSocketServer::EchoHandler, this,
            boost::placeholders::_1, boost::placeholders::_2
        ));

	}
	CWebSocketServer::~CWebSocketServer()
	{

	}
	void CWebSocketServer::EchoHandler(websocketpp::connection_hdl hdl, WebSocketServer::message_ptr msg)
	{
		CObjects objs(msg->get_payload());
        // write a new message
        mEndpoint.send(hdl, msg->get_payload(), msg->get_opcode());
    }
	bool CWebSocketServer::CreateNewSession()
	{

	}
	
	void CWebSocketServer::Run()
    {
		 mEndpoint.listen(9002);

        // Queues a connection accept operation
        mEndpoint.start_accept();

        // Start the Asio io_service run loop
        mEndpoint.run();
    }
	
	//服务管理
	void CWebSocketServer::ServicesProcess()
	{

		
	}
}